****************
*HMIS DID REGS *
****************

local controls=""
local outcomelist=" em_shelter shelter " 

cap frame change default
cap frame drop results 
frame create results strL(label) b_evict se_evict b_evictXout se_evictXout n_persons n_person_years non_evict_base_mean y_base_n 

***************
* Regressions *
***************
foreach pop in "all" "black"  "female"  {
	foreach popval of numlist 0 1 {
		if "`pop'"~="all" | `popval'~=0	{
			foreach per in "1_1"    {
				
				if "`per'"=="1_1" local did_est="3"
				
				if "`per'"=="1_1" local base="1"
				
				if "`pop'"~="all" {
					use `outcomelist' evicted qdum* evictXq* pik year black hispanic female noprior joint acs_placebo  using "${build}/linked_hmis_event_study_panel.dta" if evicted~=. & acs_placebo==0 & `pop'==`popval', clear
					gen black_miss = black == .
					gen female_miss = female == . 
					replace black = 0 if black_miss == 1
					replace female = 0 if female_miss == 1
					keep if  `pop'==`popval'
				}
				if "`pop'"=="all" use `outcomelist' evicted qdum* evictXq* pik year black hispanic female noprior joint acs_placebo  using "${build}/linked_hmis_event_study_panel.dta" if evicted~=. & acs_placebo==0, clear
				
				gen qdum3_4 = qdum3==1 | qdum4==1 if qdum3!=. & qdum4!=.
				gen qdum4_5 = qdum4==1 | qdum5==1 if qdum4!=. & qdum5!=.

				gen evictXq3_4 = evictXq3==1 | evictXq4==1
				gen evictXq4_5 = evictXq4==1 | evictXq5==1


				foreach var of varlist `outcomelist' {
				di "On Pop = `pop'=`popval', Out = `var', Period = `per'"

					if "`per'"=="1_1" {
						areg  `var' evicted qdum2-qdum5  evictXq2-evictXq5 `controls' i.year, a(pik) cluster(pik) robust
					}
					
					local label="Pop: `pop'=`popval', Outcome: `var', Outcome Window: `per'" 
					
					* Save regression output
					local b_evicted= _b[evicted]
					local se_evicted=_se[evicted]
					local b_evictXout=_b[evictXq`did_est']
					local se_evictXout=_se[evictXq`did_est']
					unique pik if evicted==0 & qdum`base'==1 & e(sample)
					local y_base_n= r(sum)
					sum `var' if evicted==0 & qdum`base'==1 & e(sample)
					local non_evict_base_mean=r(mean)
					local n_person_years=e(N) 
					capture drop first
					capture drop sample
					gen sample=e(sample)
					bys pik sample: gen first=_n==1
					count if first==1 & sample==1
					local n_persons=.
					frame post results ("`label'")  (`b_evicted') (`se_evicted') (`b_evictXout') (`se_evictXout') (`n_persons') (`n_person_years') (`non_evict_base_mean') (`y_base_n')
					
					} // outcome loop 
				} // period loop 
			} // if statement on if groups are equal to 1 (doesn't run on 0)
	} // pop = 0/1 loop
} // pop loop

frame results: export delimited using "${Inputs}/Cook_hmis_did.csv", replace 

import delimited "${Inputs}/Cook_hmis_did.csv", clear 

split label, parse(",")
rename (label1 label2 label3 label4) (population outcome time_period specification)
rename (b_evict se_evict b_evictXout se_evictXout n_obs y_base_mean) (Coeff_evict  SE_evict Coeff_evictXout SE_evictXout N_Person_Years Mean_Non_Evict_Base)
drop *judgement*

export delimited using "${Inputs}/Cook_hmis_did.csv", replace 